﻿#region Usings

using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using ScrumTable.BL.DM.DataManagement;
using ScrumTable.Common.Unity;
using ScrumTable.UI.View.Converter;
using ScrumTable.UI.View.Util;
using ScrumTable.UI.View.WinTouch.UserControls.View;
using ScrumTable.UI.ViewModel;

#endregion

namespace ScrumTable.UI.View.WinTouch.UserControls
{
    /// <summary>
    /// This class shows the right item-control for a given datacontext
    /// </summary>
    public class ScrumItemControlBack : ScrumUserControl
    {
        public ScrumItemControlBack()
        {
            DataContextChanged += OnDataContextChanged;
        }

        private void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (DataContext is UserStoryViewModel)
            {
                Content = new UserStoryControlBack() { DataContext = DataContext }; ;
            }
            else if( DataContext is BugViewModel)
            {
                Content = new BugControlBack() { DataContext = DataContext };
            }
            else if ( DataContext is TaskViewModel)
            {
                Content = new TaskControlBack() { DataContext = DataContext }; ;
            }
        }

        public static void ShowItem(IScrumItemViewModel<DomainBase> scrumItem, Point position)
        {

            var window = (AppScreen)Application.Current.MainWindow;
            string idTitle = scrumItem.BackEndId != "-1" ? " ( #" + scrumItem.BackEndId + " )" : "";

            var item = new ScrumItemControlBack
            {

                VerticalAlignment = VerticalAlignment.Bottom,
                HorizontalAlignment = HorizontalAlignment.Left,
                DataContext = scrumItem
            };

            var colorBinding = BindingUtil.CreateBindSourcePath("HasSynchronizationErrors", scrumItem, new ConverterErrorBoolToErrorColor(), BindingMode.OneWay);
            window.GetUnityContainer().Resolve<AppScreen>().AddScatteredItem(item, window.PointToScreen(position), true, false, idTitle, false, true, colorBinding);
        }
    }
}
